﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using CheckersLogic;

namespace CheckersUI
{
    public delegate void ChangeNumberOfPlayers(int i_newNumber);

    public partial class GameProperties : Form
    {
        public event ChangeNumberOfPlayers UpdateNumberOfPlayers;

        private int m_NumberOfPlayers;
        private GameBoard m_GameBoard;
        private Player m_PlayerX;
        private Player m_PlayerO;

        public GameProperties(ref GameBoard i_GameBoard, ref Player i_PlayerX, ref Player i_PlayerO, ref int i_numberOfPlayers)
        {
            this.InitializeComponent();
            this.m_GameBoard = i_GameBoard;
            this.m_GameBoard.Size = 6;
            this.m_PlayerX = i_PlayerX;
            this.m_PlayerO = i_PlayerO;
            this.m_NumberOfPlayers = i_numberOfPlayers;
        }

        private void checkBoxPlayer2_Click(object sender, EventArgs e)
        {
            if (textBoxPlayer2Name.Enabled)
            {
                textBoxPlayer2Name.Enabled = false;
            }
            else
            {
                textBoxPlayer2Name.Enabled = true; 
            }
        }

        private void buttonDone_Click(object sender, EventArgs e)
        {
            if (textBoxPlayer1Name.Text != string.Empty)
            {
                this.m_PlayerO.Name = textBoxPlayer1Name.Text;                               
            }
            else
            {
                this.m_PlayerO.Name = "Player 1";
            }

            if (checkBoxTwoPlayers.Checked == true)
            {
                this.m_NumberOfPlayers = 2;
                if (textBoxPlayer2Name.Text != string.Empty)
                {
                    this.m_PlayerX.Name = textBoxPlayer2Name.Text;
                }
                else
                {
                    this.m_PlayerX.Name = "Player 2";
                }
            }
            else
            {
                this.m_NumberOfPlayers = 1;
                this.m_PlayerX.Name = "Computer";
            }

            this.UpdateNumberOfPlayers.Invoke(this.m_NumberOfPlayers);
            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void numericUpDownBoardWidth_ValueChanged(object sender, EventArgs e)
        {
            this.m_GameBoard.Size = (int)(sender as NumericUpDown).Value;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            string path = this.GetSettingsFilePath();

            StringBuilder data = new StringBuilder();
            data.AppendLine(this.m_GameBoard.Size.ToString());
            if (checkBoxTwoPlayers.Checked)
            {
                data.AppendLine("2");
            }
            else
            {
                data.AppendLine("1");
            }

            data.AppendLine(textBoxPlayer1Name.Text);
            data.AppendLine(textBoxPlayer2Name.Text);

            System.IO.File.WriteAllText(path, data.ToString());

            MessageBox.Show("Setting saved successfully");
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            string path = this.GetSettingsFilePath();
            if (System.IO.File.Exists(path))
            {
                System.IO.StreamReader file = new System.IO.StreamReader(path);

                numericUpDownBoardWidth.Value = int.Parse(file.ReadLine());
                this.m_NumberOfPlayers = int.Parse(file.ReadLine());
                textBoxPlayer1Name.Text = file.ReadLine();
                if (this.m_NumberOfPlayers == 2)
                {
                    checkBoxTwoPlayers.Checked = true;
                    textBoxPlayer2Name.Enabled = true;
                    textBoxPlayer2Name.Text = file.ReadLine();
                }

                file.Close();
            }
            else
            {
                MessageBox.Show("No prevoius properties file exist", "Load Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private string GetSettingsFilePath()
        {
            string path = System.Environment.CurrentDirectory;
            path = path.Substring(0, path.Length - 10);
            path += "\\SavedSettings.txt";
            return path;
        }
    }
}
